package com.example.administrator.myapplication.contact;


import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Event;
import android.provider.ContactsContract.CommonDataKinds.Im;
import android.provider.ContactsContract.CommonDataKinds.Nickname;
import android.provider.ContactsContract.CommonDataKinds.Note;
import android.provider.ContactsContract.CommonDataKinds.Organization;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
import android.provider.ContactsContract.CommonDataKinds.Website;
import android.provider.ContactsContract.Data;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;

import com.example.administrator.myapplication.L;
import com.example.administrator.myapplication.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class ContactActivity2 extends Activity {

    private static final String TAG = "ContactActivity";



    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkPermissions();
        try {
            getContactInfo();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    private void checkPermissions() {
        List<String> permissionList = new ArrayList<>();
        if (ContextCompat.checkSelfPermission(ContactActivity2.this,
                Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            permissionList.add(Manifest.permission.READ_CONTACTS);
        }
        if (!permissionList.isEmpty()) {
            String[] permissions = permissionList.toArray(new String[permissionList.size()]);
            ActivityCompat.requestPermissions(ContactActivity2.this, permissions, 1);
        } else {


        }

    }

    private JSONArray contactData;
    private JSONObject jsonObject;
    public String getContactInfo() throws JSONException {
        Log.d("====te","1111111");
        // 获得通讯录信息 ，URI是ContactsContract.Contacts.CONTENT_URI
        contactData = new JSONArray();
        String mimetype = "";
        int oldrid = -1;
        int contactId = -1;
        Cursor cursor = getContentResolver().query(Data.CONTENT_URI, null, null, null, Data.RAW_CONTACT_ID);
        int numm = 0;
        while (cursor.moveToNext()) {
            contactId = cursor.getInt(cursor.getColumnIndex(Data.RAW_CONTACT_ID));
            if (oldrid != contactId) {
                jsonObject = new JSONObject();
              /*  if (!TextUtils.isEmpty(jsonObject.get("name").toString())&&!TextUtils.isEmpty(jsonObject.get("phone").toString())) {
                    contactData.put(jsonObject) ;

                    Log.v("=====cccddd",numm+"==="+jsonObject.get("name").toString() + "====" +jsonObject.get("phone").toString());
                }*/

//                Log.d("=====sssdd","111111111111111");

                contactData.put(jsonObject) ;
//                contactData.put("contact" + numm, jsonObject);
                numm++;
                oldrid = contactId;
            }
            Log.d("=====sssdd",contactId+"");
            // 取得mimetype类型
            mimetype = cursor.getString(cursor.getColumnIndex(Data.MIMETYPE));
            // 获得通讯录中每个联系人的ID
            // 获得通讯录中联系人的名字
            if (StructuredName.CONTENT_ITEM_TYPE.equals(mimetype)) {
                String lastname = cursor.getString(cursor.getColumnIndex(StructuredName.GIVEN_NAME));
                jsonObject.put("name", lastname);
                if (jsonObject.has("name")) {
                    Log.d("=====sss",jsonObject.getString("name")+"==="+numm);
                }

            }
            // 获取电话信息
            if (Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
                // 取出电话类型
                int phoneType = cursor.getInt(cursor.getColumnIndex(Phone.TYPE));
                // 手机
                if (phoneType == Phone.TYPE_MOBILE) {
                    String mobile = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
                    jsonObject.put("phone", mobile);

                    Log.d("=====sss",jsonObject.getString("phone")+"==="+numm);
                }

            }
        }
        cursor.close();
        L.i(contactData.toString());
        return contactData.toString();
    }
}